package com.czmini.cztest.util;

import com.czmini.cztest.entity.ZcTestJenkins;
import org.springframework.util.StringUtils;

/**
 * @ProjectName: cztest
 * @Package: com.czmini.cztest.util
 * @ClassName: ReportUtil
 * @Author: 还是那个橙子
 * @Description: 测试报告获取工具类
 * @Date: 2022/5/15 12:13
 * @Version: 1.0
 */

public class ReportUtil {

    public static void main(String[] args) {
        String buildUrl = "http:///job/hogwarts_test_mini_start_test_1/label=jenkins_slave/2/allure/";
        String allureReportBaseUrl = buildUrl.substring(buildUrl.indexOf("/job"));
        System.out.println("allureReportBaseUrl== "+allureReportBaseUrl);
    }

    /**
     * @param buildUrl 构建地址
     * @param zcTestJenkins Jenkins 对象
     * @param autoLoginJenkinsFlag  是否自动登录Jenkins
     * @return获取allure 报告
     */
    public static String getAllureReportUrl(String buildUrl, ZcTestJenkins zcTestJenkins,boolean autoLoginJenkinsFlag) {
        if (StringUtils.isEmpty(buildUrl)||!buildUrl.contains("/job")) {
            return buildUrl;
        }
        String allureReportBaseUrl = buildUrl;

        if(autoLoginJenkinsFlag){
            allureReportBaseUrl = getAllureReportUrlAndLogin(buildUrl, zcTestJenkins);
        }
        return allureReportBaseUrl + "allure-report";
    }

    /**
     * 获取可以自动登录Jenkins的allure报告地址
     * @param buildUrl
     * @param zcTestJenkins
     * @return
     */
    private static String getAllureReportUrlAndLogin(String buildUrl, ZcTestJenkins zcTestJenkins) {
        String allureReportUrl;
        // url 拼接可以报告地址
        String allureReportBaseUrl =zcTestJenkins.getUrl()+"j_acegi_security_check?j_username="+zcTestJenkins.getUserName()
                +"&j_password="+zcTestJenkins.getPassword()+"&Submit=登录&remember_me=on"+"&from=";
        allureReportUrl = allureReportBaseUrl + buildUrl.substring(buildUrl.indexOf("/job"));
        return allureReportUrl;

    }


}
